package util

import (
	"fmt"
	"strings"
	"yunj/pkg/global"

	"github.com/gin-gonic/gin"
)

// 构建后台地址
func BuildAdminUrl(path string, vars ...map[string]any) string {
	url := BuildUrl(path, vars...)
	adminEntrance := global.Config.GetAppAdminEntrance()
	if !strings.HasPrefix(url, adminEntrance) {
		url = adminEntrance + url
	}
	return url
}

// 重定向到提示页面
func RedirectAdminTips(ctx *gin.Context, errcode int, msg ...string) {
	tipsVars := map[string]any{"code": errcode}
	if isPopup := QueryParam(ctx, "isPopup", ""); isPopup == "yes" {
		tipsVars["isPopup"] = "yes"
	}
	url := BuildAdminUrl("/tips", tipsVars)
	if len(msg) > 0 && msg[0] != "" {
		url += fmt.Sprintf("&msg=%s", msg[0])
	}
	Redirect(ctx, url)
}
